package com.hertz.iris.ui;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ArgbEvaluator;
import com.nineoldandroids.animation.ObjectAnimator;

public class CardFlipHelper {
	public static final int[] DEFAULT_2_SUCCESS = {0xff000000,0xff000000,0xff000000,0xff2ecc71,0xff2ecc71};
	public static final int[] SUCCESS_2_DEFAULT = {0xff2ecc71,0xff2ecc71,0xff2ecc71,0xff000000,0xff000000};
	
	public final List<int[]> themeList = new ArrayList<int[]>();
	
	public static final byte DEFAULT_THEME = 1;
	
	/*
	 * 500
	 */
	public static final long DEFAULT_DURATION = 500;
	
	public CardFlipHelper(){
		themeList.add(null);
		themeList.add(DEFAULT_2_SUCCESS);
		themeList.add(SUCCESS_2_DEFAULT);
	}
	
	public void packCardFlip(byte theme,View view){
		packCardFlip(CardFlipHelper.DEFAULT_DURATION, theme, view);
	}
	
	public void packCardFlip(long duration,byte theme,View view){
		if(view == null)
			return;
		byte next = next(view,theme);
		int[] changeValues = themeList.get(next);
		packCardFlip(duration, view, changeValues);
	}
	
	public void packCardFlip(long duration,View view,int ... colors){
		if(view == null)
			return;
		AnimatorSet set = new AnimatorSet();
		ObjectAnimator rotationxAnim = ObjectAnimator.ofFloat(view, "rotationX", 0, 180,0);
		rotationxAnim.setInterpolator(new AccelerateDecelerateInterpolator());
		
		ObjectAnimator colorAnim = null;
		if(colors != null && colors.length != 0){
			colorAnim = ObjectAnimator.ofInt(view, "textColor", colors);
			colorAnim.setEvaluator(new ArgbEvaluator());
		}
		
		if(colorAnim != null){
			set.playTogether(rotationxAnim,colorAnim);
		}else{
			set.playTogether(rotationxAnim);
		}
		set.setDuration(duration).start();
	}
	
	public void packCardFlip(View view,int ... colors){
		packCardFlip(CardFlipHelper.DEFAULT_DURATION, view, colors);
	}
	
	private byte next(View view,byte theme){
		if(view.isSelected()){
			return theme;
		}else{
			byte next = Integer.valueOf(theme + 1).byteValue();
			return next;
		}
	}
	
}
